package Utils;

import Monitor.ClientThread;
import Service.LoginOut;

import java.util.HashMap;

/**
 * The type Thread manage.
 *
 * @author dzr
 * @version 1.0
 */
public class ThreadManage {
    private static ThreadManage instance;
    private final HashMap<Integer, ClientThread> clientThreads = new HashMap<>(0);
    private ThreadManage(){}

    /**
     * Gets instance.
     *
     * @return the instance
     */
    public static ThreadManage getInstance() {
        if(instance == null){
            instance = new ThreadManage();
        }
        return instance;
    }

    /**
     * Get specific client thread.
     *
     * @param uid the uid
     * @return the client thread
     */
    public ClientThread getClientThread(int uid){
        return clientThreads.get(uid);
    }

    /**
     * Close specific client thread.
     *
     * @param uid the uid
     */
    public void closeClientThread(int uid){
        LoginOut.loginOut(uid);
        getClientThread(uid).setExitStatus(true);
        ChatManage.closeAllChat();
    }

    /**
     * Gets all client threads.
     *
     * @return the client threads
     */
    public HashMap<Integer, ClientThread> getClientThreads() {
        return clientThreads;
    }

    /**
     * Add new client thread.
     *
     * @param uid          the uid
     * @param clientThread the client thread
     */
    public void addClientThread(int uid, ClientThread clientThread){
        clientThreads.put(uid, clientThread);
    }
}
